﻿/*----------------------------------------------------------------
// Copyright (C) 2011 周士祎
// 版权所有。 
//
// 文件名：DataAccessMenu
// 文件功能描述：模块菜单数据层
//
// 
// 创建标识：2012-01-03 周士祎
//
// 修改标识：
// 修改描述：
//
//
//----------------------------------------------------------------*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Collections;
using DbAdapter.sqldbAdapter;

namespace DAO.WaiGaoQiao
{
    public class DataAccessModular
    {
        public DataAccessModular()
        { }

        public DataSet GetModular()
        {
            sqldb db = new sqldb();
            string query = "select ModularId,ModularName,ModularUrl,DisplayOrder "
                         + "from Tbt_Modular order by DisplayOrder";
            DataSet ds = db.ExecuteDataset(GetConntionString.conntionstring(), CommandType.Text, query);
            return ds;
        }
        /// <summary>
        /// 根据角色获取模块
        /// </summary>
        /// <param name="roleid"></param>
        /// <returns></returns>
        public DataSet GetModularByRole(string roleid)
        {
            sqldb db = new sqldb();
            string query = "select ModularId,ModularName,ModularUrl,DisplayOrder from Tbt_Modular where ModularId in ("
                         + "select ModularId from Tbt_RoleAndModular where RoleId = " + roleid + ")";
            DataSet ds = db.ExecuteDataset(GetConntionString.conntionstring(), CommandType.Text, query);
            return ds;
        }
        /// <summary>
        /// 先清理绑定的角色模块
        /// </summary>
        /// <param name="roleid"></param>
        /// <returns></returns>
        public int ClearBindModularToRole(string roleid)
        {
            sqldb db = new sqldb();
            string query = "delete from Tbt_RoleAndModular where RoleId = '" + roleid+"'";                       
            return db.ExecuteNonQuery(GetConntionString.conntionstring(), CommandType.Text, query);
        }
        /// <summary>
        /// 绑定模块到角色
        /// </summary>
        /// <param name="mid"></param>
        /// <param name="logid"></param>
        /// <returns></returns>
        public int BindModularToRole(string mid, string roleid)
        {
            sqldb db = new sqldb();
            string query = "insert into Tbt_RoleAndModular(RoleId,ModularId) "
                         + "values('" + roleid + "','" + mid + "')";
            return db.ExecuteNonQuery(GetConntionString.conntionstring(), CommandType.Text, query);
        }
        /// <summary>
        /// 删除绑定
        /// </summary>
        /// <param name="mid"></param>
        /// <param name="logid"></param>
        /// <returns></returns>
        public int NotBindModular(string mid, string roleid)
        {
            sqldb db = new sqldb();
            string query = "delete from Tbt_RoleAndModular where RoleId = '" + roleid
                         + "' and ModularId = '" + mid + "'";
            return db.ExecuteNonQuery(GetConntionString.conntionstring(), CommandType.Text, query);
        }

        /// <summary>
        /// 根据登录名称取模块
        /// </summary>
        /// <param name="logid"></param>
        /// <returns></returns>
        public DataSet GetModular(string logid)
        {
            sqldb db = new sqldb();
            //string query = "select C.* from Tbt_Modular C inner join Tbt_RoleAndModular B ON C.ModularId = B.ModularId "
            //              +"inner join Tbt_LogUserAndRole A ON B.RoleId = A.RoleId "
            //              +"where A.LogName = '" + logid.Trim() + "' order by C.DisplayOrder";
            string query = "select * from Tbt_Modular D "
                          + "WHERE ModularId in ("
                          + "select distinct C.ModularId from Tbt_SitMap  C inner join Tbt_RoleAndMenu B ON C.NodeID = B.MenuId "
                          + "inner join Tbt_LogUserAndRole A ON B.RoleId = A.RoleId "
                          + "where A.LogName = '" + logid.Trim() + "') order by D.DisplayOrder";
            DataSet ds = db.ExecuteDataset(GetConntionString.conntionstring(), CommandType.Text, query);
            return ds;
        }
    }
}
